#pragma once
#include <unordered_map>
#include <queue>
#include "log_def.h"
#include "libevent_cpp/include/include_all.h"
#include "svr_util/include/su_include.h"
#include "svr_util/include/time/su_timer.h"
#include "../acc_proto/include/proto.h"

//连接外网client的sever connect
class ExternalSvrCon : public lc::SvrCon
{
private:
	static const uint64 VERIFY_TIME_OUT_SEC = 10;
	enum class State
	{
		WAIT_1ST_MSG,		//等请求验证消息，接收消息会转发验证svr。并->WAIT_VERIFY
		WAIT_VERIFY,		//等验证结果。成功->VERIFYED,失败->WAIT_1ST_MSG
		VERIFYED,			//验证已经通过
		WAIT_DISCON,       //连接超上限，等删除
	};

	State m_state = State::WAIT_1ST_MSG;
	std::array<uint16, proto::MAX_MAIN_CMD_ID> m_svrTypeIdxSvrId = {};//svrType index 当前激活的 svrId. 
	bool m_isCache = false;			//true 表示缓存消息，暂停转发。
	std::vector<std::string> m_cacheMsg;

	lc::Timer m_wfm_tm;			    //等第一条消息超时定时器
	lc::Timer m_verify_tm;		    //认证超时定时器
	lc::Timer m_heartbeat_tm;	    //心跳超时定时器
	lc::Timer m_cls_tm;			    //client limite size. 最大client数量，断开定时器
	time_t    m_lhtm = 0;		    //last heartbeat timestamp;

public:
	ExternalSvrCon();
	virtual ~ExternalSvrCon() override;
	void SetVerify(bool is_success);
	bool IsVerify() const;
	//发送 client和svr层：cmd,msg 到client
	bool SendMsg(uint32 cmd, const std::string &msg);
	bool SetSession(uint16 mainCmd, uint16 svrId); //设置与svr会话
	void SetCache(bool isCache);
	void DelSession(uint16 mainCmd, uint32 svrId);
	bool IsConSvr(uint16 mainCmd, uint16 svrId) const; //true表示和svrId 有会话

private:
	virtual void OnConnected() override;
	virtual void OnRecv(const lc::MsgPack &msg)override;

private:
	void Forward2VerifySvr(const lc::MsgPack& msg);
	void Forward2Svr(const lc::MsgPack& msg);
};